class Parent extends React.Component {
  constructor() {
    super();
    console.log("父组件：constructor");
  }
  state = {
    userName: "gqg",
  };
  static getDerivedStateFromProps() {
    console.log("父组件：getDerivedStateFromProps");
    return null;
  }
  componentDidMount() {
    console.log("父组件：componentDidMount");
  }
  shouldComponentUpdate(nextProps, nextState) {
    console.log("父组件：shouldComponentUpdate");
    return true;
  }

  getSnapshotBeforeUpdate(prevProps, prevState) {
    console.log("父组件：getSnapshotBeforeUpdate");
    return null;
  }

  componentDidUpdate(prevProps, prevState, snapshot) {
    console.log("父组件：componentDidUpdate");
  }

  componentWillUnmount() {
    console.log("父组件：componentWillUnmount");
  }
  handleParent = () => {
    this.setState({
      userName: "mwc",
    });
  };
  render() {
    console.log("父组件：render");
    return (
      <div>
        <h1>我是父组件{this.state.userName}</h1>
        <Child></Child>
        <button onClick={this.handleParent}>更改父组件的状态</button>
      </div>
    );
  }
}

class Child extends React.Component {
  constructor() {
    super();
    console.log("子组件：constructor");
  }
  state = {
    num: 0,
  };
  static getDerivedStateFromProps() {
    console.log("子组件：getDerivedStateFromProps");
    return null;
  }
  componentDidMount() {
    console.log("子组件：componentDidMount");
  }
  shouldComponentUpdate(nextProps, nextState) {
    console.log("子组件：shouldComponentUpdate");
    return true;
  }
  getSnapshotBeforeUpdate(prevProps, prevState) {
    console.log("子组件：getSnapshotBeforeUpdate");
    return null;
  }
  componentDidUpdate(prevProps, prevState, snapshot) {
    console.log("子组件：componentDidUpdate");
  }
  componentWillUnmount() {
    console.log("子组件：componentWillUnmount");
  }
  handleChild = () => {
    this.setState({
      num: 2,
    });
  };
  render() {
    console.log("子组件：render");
    return (
      <div>
        <h3>我是子组件{this.state.num}</h3>
        <button onClick={this.handleChild}>更改子组件的状态</button>
      </div>
    );
  }
}

export default Parent